package main

import (
	"fmt"
	"github.com/hibiken/asynq"
	"go-learn/go_learn/asyncq/task"
	"log"
)

// workers.go
func main() {
	srv := asynq.NewServer(
		asynq.RedisClientOpt{Addr: "localhost:6379", Password: "123456", DB: 1},
		asynq.Config{Concurrency: 10},
	)

	mux := asynq.NewServeMux()
	mux.HandleFunc(task.TypeWelcomeEmail, task.HandleWelcomeEmailTask)
	mux.HandleFunc(task.TypeReminderEmail, task.HandleReminderEmailTask)

	go func() {
		scheduler := asynq.NewScheduler(asynq.RedisClientOpt{Addr: "127.0.0.1:6379", Password: "123456", DB: 1}, nil)
		task, err := task.NewWelcomeEmailTask(43)
		if err != nil {
			log.Fatal(err)
		}
		// entryId, err := scheduler.Register("*/1 * * * *", task)
		entryId, err := scheduler.Register("@every 3s", task)
		if err != nil {
			log.Fatal(err)
		}
		fmt.Println("scheduler entryid:", entryId)
		err = scheduler.Run()
		if err != nil {
			log.Fatal(err)
		}
	}()

	if err := srv.Run(mux); err != nil {
		log.Fatal(err)
	}
}
